package com.example.mylibrary;

import static android.content.ContentValues.TAG;
import android.app.Instrumentation;
import android.util.Log;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class BarterPersonalCenterPage {
    public static UiDevice device;
    public static Instrumentation instrumentation;

    @Before
    public void first() throws InterruptedException{
        Log.i(TAG,"连接设备");
        instrumentation=
                InstrumentationRegistry.getInstrumentation();
        device= UiDevice.getInstance(instrumentation);
        int width = device.getDisplayWidth();
        int height = device.getDisplayHeight();
        device.pressHome();
        Thread.sleep(2000);
        device.swipe(width-10,height/2,50,height/2,10);
        Thread.sleep(2000);
        UiObject2 barter = device.findObject(By.text("Barter"));
        barter.click();
        Thread.sleep(3000);
    }

//    登录
    private void login() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).setText("2019012514");
        Thread.sleep(100);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).click();
        Thread.sleep(1000);
    }

//    个人中心页面，修改个人信息
    @Test
    public void testPersonalCenter() throws InterruptedException {
        login();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/ib_personal_center")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_user_info")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_modify_info")).click();
        Thread.sleep(1000);
        device.findObject(By.clazz("com.leaf.collegeidleapp:id/et_stu_name")).setText("zhangsan");
        Thread.sleep(1000);
        device.findObject(By.clazz("com.leaf.collegeidleapp:id/et_stu_major")).setText("software");
        Thread.sleep(1000);
        device.findObject(By.clazz("com.leaf.collegeidleapp:id/et_stu_phone")).setText("13231154989");
        Thread.sleep(1000);
        device.findObject(By.clazz("com.leaf.collegeidleapp:id/et_stu_qq")).setText("619654789");
        Thread.sleep(1000);
        device.findObject(By.clazz("com.leaf.collegeidleapp:id/et_stu_address")).setText("诚朴园三号楼");
        Thread.sleep(1000);
        device.findObject(By.clazz("com.leaf.collegeidleapp:id/btn_save_info")).click();
        Thread.sleep(1000);
    }


//    个人中心页面，我的发布
    @Test
    public void testMyRelease() throws InterruptedException {
        login();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/ib_personal_center")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_my_goods")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/iv_commodity")).longClick();
        Thread.sleep(1000);
        device.findObject(By.res("android:id/button1")).click();
        Thread.sleep(1000);
    }

//    个人中心页面，我的收藏

    @Test
    public void testMyCollections() throws InterruptedException {
        login();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/ib_personal_center")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_my_collection")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/iv_commodity")).longClick();
        Thread.sleep(1000);
        device.findObject(By.res("android:id/button1")).click();
        Thread.sleep(1000);
    }

//    个人中心页面，修改密码
    @Test
    public void testChangePwd() throws InterruptedException {
        login();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/ib_personal_center")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_modify_password")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_original_pwd")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_new_pwd")).setText("12345678");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_new_pwd")).setText("12345678");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_modify_pwd")).click();
        Thread.sleep(1000);
}

    @After
    public static void tearDown() {
        device.pressBack();
    }
}
